package demo;
import static java.lang.Thread.sleep;
public class MyThread07 {
    private static class MyRunnable implements Runnable{
        @Override
        public void run() {

              while(!Thread.interrupted()){
                  System.out.println(Thread.currentThread().getName()+"正在工作");

                  try {
                      sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                      break;//收到打断信号就跳出循环不工作
                  }

              }

            System.out.println(Thread.currentThread().getName()+"不工作了");
        }
    }
    public static void main(String[] args) throws InterruptedException {

            Thread t=new Thread(new MyRunnable(),"线程1");
            t.start();
            System.out.println(Thread.currentThread().getName()+"开始工作");
            Thread.sleep(5*1000);
            t.interrupt();
    }
}
